package com.ibiaoke.goods.service;

import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.exception.ServiceException;
import com.ibiaoke.common.utils.DateUtils;
import com.ibiaoke.common.utils.RandomUtils;
import com.ibiaoke.goods.dao.XcxReduceGoodsDao;
import com.ibiaoke.goods.dao.XcxReduceGoodsDetailDao;
import com.ibiaoke.goods.dao.XcxShopPriceDao;
import com.ibiaoke.goods.entity.XcxReduceGoods;
import com.ibiaoke.goods.entity.XcxReduceGoodsDetail;
import com.ibiaoke.goods.entity.XcxShopPrice;
import com.ibiaoke.goods.entity.vo.XcxReduceGoodsDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class XcxReduceGoodsServiceImpl implements  XcxReduceGoodsService{

    @Autowired
    private XcxReduceGoodsDao xcxReduceGoodsDao;

    @Autowired
    private XcxReduceGoodsDetailDao xcxReduceGoodsDetailDao;

    @Autowired
    private XcxShopPriceDao xcxShopPriceDao;

    @Transactional
    @Override
    public Integer addXcxReduceGoods(String userName, List<XcxReduceGoodsDetailVo> xcxReduceGoodsDetailVo,
                    Integer payType,Double orderPrice,Double discounPrice,Double freightPrice,Double realPrice,Integer shopId,String shopName
                    ,Double onePrice) {
        Integer i = null;
        XcxReduceGoods xcxReduceGoods = new XcxReduceGoods();
        xcxReduceGoods.setPayType(payType);
        xcxReduceGoods.setOrderPrice(new BigDecimal(orderPrice));
        xcxReduceGoods.setDiscounPrice(new BigDecimal(discounPrice));
        xcxReduceGoods.setFreightPrice(new BigDecimal(freightPrice));
        xcxReduceGoods.setRealPrice(new BigDecimal(realPrice));
        xcxReduceGoods.setShopId(shopId);
        //增加减去库存记录表
        xcxReduceGoods.setReduceCode(generateOrderNo());
        xcxReduceGoods.setOperator(userName);
        xcxReduceGoods.setCreateTime(new Date());
        xcxReduceGoodsDao.insert(xcxReduceGoods);
        //增加购物车表，关联上减去库存记录表
        for(XcxReduceGoodsDetailVo vo : xcxReduceGoodsDetailVo){
            XcxReduceGoodsDetail detail =new XcxReduceGoodsDetail();
            detail.setReduceCode(xcxReduceGoods.getReduceCode());
            detail.setGoodsId(vo.getGoodsId());
            detail.setGoodsName(vo.getGoodsName());
            detail.setGoodsSpec(vo.getGoodsSpec());
            detail.setGoodsImage(vo.getGoodsImage());
            detail.setNum(vo.getNum());
            detail.setPrice(orderPrice);
            detail.setShopid(shopId);
            detail.setShopName(shopName);
            detail.setOnePrice(onePrice);
            detail.setDelStatus(0);
            detail.setCreateTime(new Date());
            detail.setUpdateTime(new Date());
            xcxReduceGoodsDetailDao.insert(detail);
            //检验库存
            XcxShopPrice xcxShopPrice = xcxShopPriceDao.findGood(xcxReduceGoods.getShopId(),detail.getGoodsId());
            if(xcxShopPrice.getStockNum() < detail.getNum()){
                throw new ServiceException("库存不足，无法完成操作");
            }
            //减去库存
             i = xcxShopPriceDao.updateXcxGoodStockNum1(detail.getGoodsId(),xcxReduceGoods.getShopId(),
                    new BigDecimal(detail.getNum().toString()).multiply(new BigDecimal("-1")).doubleValue());
        }
        return i;
    }

    @Override
    public List<XcxReduceGoods> findXcxReduceGoods(Integer shopId,Page page) {
        if(shopId == null ){
            throw new ServiceException("店铺Id不能为空");
        }
        return xcxReduceGoodsDao.findXcxReduceGoods(shopId,page);
    }

    @Override
    public List<XcxReduceGoodsDetail> findXcxReduceGoodsDetail(String reduceCode) {
        if(reduceCode == null ){
            throw new ServiceException("订单号不能为空");
        }
        return xcxReduceGoodsDetailDao.findXcxReduceGoodsDetail(reduceCode);
    }

    public String generateOrderNo() {
        String dateNo = DateUtils.formatOrderNoDate(new Date());
        return dateNo + RandomUtils.generatorOrderNo();
    }
}